package com.ifcar.app.rss;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.ifcar.app.util.HttpUtils;
import com.ifcar.app.util.NeteaseHttpClient;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.Tools;
import com.jakewharton.DiskLruCache;

public class GetContent {	
    
	/**
	 * 获取态度，另一面的内容列表
	 * @param url 交互地址
	 * @param startNumber 开始记录下标
	 * @param perTimeNumber 记录条数
	 * @return List<Map<String, Object>> 符合条件的条数
	 */
	public static List<Map<String, Object>> getAttitudeList(String url,	int startNumber, int perTimeNumber, Context c) {
		
		List<Map<String, Object>> list = null;

		JSONArray jsonArray = null;
		JSONObject jobj = null;
		HashMap<String, Object> map = null;

		StringBuilder currentUrl = new StringBuilder();
		currentUrl.append(url).append(startNumber).append("-").append(perTimeNumber).append(".html");
		
		String results = HttpUtils.getResponseFromGetUrl(currentUrl.toString(), 30000,c);

		if (results != null && !results.equals("")){
			try {
				jobj = new JSONObject(results);
				if(jobj != null){
				    if (startNumber == 0) {
	                    StringUtil.updateRefreshTime(c, StringUtil.TOPIC);
				    }

					jsonArray = jobj.getJSONArray(StringUtil.TOPIC);
					list = new ArrayList<Map<String, Object>>();
					for (int i = 0; jsonArray != null && i < jsonArray.length(); i++) {
						jobj = jsonArray.getJSONObject(i);
						map = new HashMap<String, Object>();
						map.put(StringUtil.TOPIC_IMGSRC, jobj.getString(StringUtil.TOPIC_IMGSRC));
						map.put(StringUtil.TOPIC_DOCID, jobj.getString(StringUtil.TOPIC_DOCID));
						map.put(StringUtil.TOPIC_TITLE, jobj.getString(StringUtil.TOPIC_TITLE));
						String subtitle = jobj.isNull(StringUtil.TOPIC_SUBTITLE)?"":jobj.getString(StringUtil.TOPIC_SUBTITLE);
						map.put(StringUtil.TOPIC_SUBTITLE, subtitle);
						map.put(StringUtil.TOPIC_PTIME, jobj.getString(StringUtil.TOPIC_PTIME));
						map.put(StringUtil.TOPIC_URL, jobj.getString(StringUtil.TOPIC_URL));
						String digest = jobj.isNull(StringUtil.TOPIC_DIGEST)?"":jobj.getString(StringUtil.TOPIC_DIGEST);
						digest = digest.length()>30?digest.substring(0, 28)+"..":digest;
                        map.put(StringUtil.TOPIC_DIGEST, Tools.htmlDecoder(digest));
						map.put(StringUtil.TOPIC_SPECIAL_ID, jobj.isNull(StringUtil.TOPIC_SPECIAL_ID)?"":jobj.getString(StringUtil.TOPIC_SPECIAL_ID));
						
						list.add(map);
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				list = null;
                NeteaseHttpClient.removeCache(c, currentUrl.toString());
			}
		}

		if (startNumber == 0 && list != null && list.size() > 0) {
		    StringUtil.updateRefreshTime(c, "attitude");
		}
		
		return list;
	}


public static List<HashMap<String, Object>> getTopicList(String url,    int startNumber, int perTimeNumber, Context c) {
    List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

    JSONArray jsonArray = null;
    JSONObject jobj = null;
    HashMap<String, Object> map = null;

    StringBuilder currentUrl = new StringBuilder();
    currentUrl.append(url).append(startNumber).append("-").append(perTimeNumber).append(".html");
    
    String results = HttpUtils.getResponseFromGetUrl(currentUrl.toString(), 30000,c);

    if (results != null && !results.equals("")){
        try {
            jobj = new JSONObject(results);
            if(jobj != null){
                if (startNumber == 0) {
                    StringUtil.updateRefreshTime(c, StringUtil.TOPIC);
                }

                jsonArray = jobj.getJSONArray(StringUtil.TOPIC);
                for (int i = 0; jsonArray != null && i < jsonArray.length(); i++) {
                    jobj = jsonArray.getJSONObject(i);
                    map = new HashMap<String, Object>();
                    map.put(StringUtil.TOPIC_IMGSRC, jobj.getString(StringUtil.TOPIC_IMGSRC));
                    map.put(StringUtil.TOPIC_DOCID, jobj.getString(StringUtil.TOPIC_DOCID));
                    map.put(StringUtil.TOPIC_TITLE, jobj.getString(StringUtil.TOPIC_TITLE));
                    String subtitle = jobj.isNull(StringUtil.TOPIC_SUBTITLE)?"":jobj.getString(StringUtil.TOPIC_SUBTITLE);
                    map.put(StringUtil.TOPIC_SUBTITLE, subtitle);
                    map.put(StringUtil.TOPIC_PTIME, jobj.getString(StringUtil.TOPIC_PTIME));
                    map.put(StringUtil.TOPIC_URL, jobj.getString(StringUtil.TOPIC_URL));
                    String digest = jobj.isNull(StringUtil.TOPIC_DIGEST)?"":jobj.getString(StringUtil.TOPIC_DIGEST);
                    digest = digest.length()>26?digest.substring(0, 24)+"..":digest;
                    map.put(StringUtil.TOPIC_DIGEST, Tools.htmlDecoder(digest));
                    map.put(StringUtil.TOPIC_SPECIAL_ID, jobj.isNull(StringUtil.TOPIC_SPECIAL_ID)?"":jobj.getString(StringUtil.TOPIC_SPECIAL_ID));
                    
                    list.add(map);
                }
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            NeteaseHttpClient.removeCache(c, currentUrl.toString());
        }
    }

    if (startNumber == 0 && list.size() > 0) {
        StringUtil.updateRefreshTime(c, "attitude");
    }
    
    
    return list;
    }

	/**
	 * 获取投票列表
	 * @param url 交互地址
	 * @return List<Map<String, Object>> 符合条件的条数
	 */
	public static List<Map<String, Object>> getVoteList(String url,Context mContext) {
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		JSONArray jArray = null;
		Map<String, Object> map = null;

		String results = HttpUtils.getResponseFromGetUrl(url, 30000,mContext);
		
        if (results != null && !results.equals("")) {
            try {
                jArray = new JSONArray(results);
                if (jArray != null) {
                    final int length = jArray.length();
                    for (int i = 0; i < length; i++) {
                        JSONObject jobj = jArray.getJSONObject(i);
                        JSONArray jsonarray_voteid = jobj.getJSONArray(StringUtil.VOTE_ID_LIST); // 投票ID数组
                        JSONArray jsonarray_voteitem = jobj.getJSONArray(StringUtil.VOTE_ITEM_LIST); // 投票选项数组
                        map = new HashMap<String, Object>();

                        List<Map<String, Object>> itemlist = new ArrayList<Map<String, Object>>();
                        for (int index = 0; index < jsonarray_voteitem.length(); index++) { // 添加投票选项
                            JSONObject o = jsonarray_voteitem.getJSONObject(index);
                            Map<String, Object> item = new HashMap<String, Object>();
                            item.put(StringUtil.VOTE_ITEM_ID, o.getString(StringUtil.VOTE_ITEM_ID));
                            item.put(StringUtil.VOTE_ITEM_NUM, o.getString(StringUtil.VOTE_ITEM_NUM));

                            String title = o.getString(StringUtil.VOTE_ITEM_NAME);
                            item.put(StringUtil.VOTE_ITEM_NAME, title.length() > 18 ? title
                                    .substring(0, 16)
                                    + ".." : title);
                            
                            itemlist.add(item);
                        }
                        map.put(StringUtil.VOTE_ITEM_LIST, itemlist); // 添加投票选项
                        map.put(StringUtil.VOTE_ID, jsonarray_voteid.getString(i));
                        map.put(StringUtil.VOTE_IMGSRC, jobj.getString(StringUtil.VOTE_IMGSRC)); // 添加投票图片连接
                        map.put(StringUtil.VOTE_DIGEST, jobj.getString(StringUtil.VOTE_DIGEST)); // 添加投票描述
                        map.put(StringUtil.VOTE_QUESTION, jobj.getString(StringUtil.VOTE_QUESTION)); // 添加投票问题
                        map.put(StringUtil.VOTE_DATE, jobj.getString(StringUtil.VOTE_DATE)); // 添加投票时间

                        map.put(StringUtil.VOTE_DOCID,
                                jobj.getString(StringUtil.VOTE_DOCID) == null ? "" : jobj
                                        .getString(StringUtil.VOTE_DOCID)); // 添加投票相关文章
                        list.add(map); // 添加第一个投票
                    }
                    
                    if (list != null && list.size() > 0) {
                        StringUtil.updateRefreshTime(mContext, "vote");
                    }

                }
            } catch (Exception e) {
                list.clear();
                e.printStackTrace();
                NeteaseHttpClient.removeCache(mContext, url);
            }
		}
		
		return list;
	}

	/**
	 * 更加以往投票ID获取以往投票的数据
	 * @param oldId
	 * @return
	 */
//	private static Map<String, Object> getMapItem(String oldId,Context mContext) {
//		Map<String,Object> map = new HashMap<String,Object>();
//		String url = StringUtil.voteOld_url + oldId + ".html";
//		String results = HttpUtils.getResponseFromGetUrl(url, 30000,mContext);
//		
//		if (results != null && !results.equals("")){
//			JSONObject jobj = null;
//			try {
//				jobj = new JSONObject(results);
//				if(jobj != null){
//					JSONArray jsonarray_voteitem = jobj.getJSONArray(StringUtil.VOTE_ITEM_LIST); // 投票选项数组
//					map = new HashMap<String, Object>();
//					
//					List<Map<String,Object>> itemlist = new ArrayList<Map<String,Object>>();
//					for(int index=0;index<jsonarray_voteitem.length();index++){ // 添加投票选项
//						JSONObject o = jsonarray_voteitem.getJSONObject(index);
//						Map<String,Object> item = new HashMap<String, Object>();
//						item.put(StringUtil.VOTE_ITEM_ID, o.getString(StringUtil.VOTE_ITEM_ID));
//						item.put(StringUtil.VOTE_ITEM_NUM, o.getString(StringUtil.VOTE_ITEM_NUM));
//						
//						String title = o.getString(StringUtil.VOTE_ITEM_NAME);						
//						item.put(StringUtil.VOTE_ITEM_NAME, title.length()>18?title.substring(0, 16)+"..":title);
//						
//						itemlist.add(item);
//					}
//					map.put(StringUtil.VOTE_ITEM_LIST, itemlist); // 添加投票选项
//
//					map.put(StringUtil.VOTE_ID, oldId);
//					map.put(StringUtil.VOTE_IMGSRC, jobj.getString(StringUtil.VOTE_IMGSRC)); // 添加投票图片连接
//					map.put(StringUtil.VOTE_DIGEST, jobj.getString(StringUtil.VOTE_DIGEST)); // 添加投票描述
//					map.put(StringUtil.VOTE_QUESTION, jobj.getString(StringUtil.VOTE_QUESTION)); // 添加投票问题
//					map.put(StringUtil.VOTE_DATE, jobj.getString(StringUtil.VOTE_DATE)); // 添加投票时间
//					map.put(StringUtil.VOTE_DOCID, jobj.getString(StringUtil.VOTE_DOCID)==null?"":jobj.getString(StringUtil.VOTE_DOCID)); // 添加投票相关文章
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//				NeteaseHttpClient.removeCache(mContext, url);
//			}
//		}
//		return map;
//	}
	
	/**
	 * 投票
	 * @param voteId 投票ID
	 * @param itemId 选项ID
	 * @return
	 */
	public static String doVote(String voteId, String itemId,Context mContext) {
		String flag = null;
		
		StringBuilder url = new StringBuilder();
		
		url.append(StringUtil.voteSubmit_Url).append(voteId).append("=").append(itemId).append("&voteId=").append(voteId);

		flag = HttpUtils.getResponseFromGetUrl(url.toString(), 30000,mContext);
		
		return flag;
	}

	/**
	 * 获取文章列表
	 * @param attitudeUrl
	 * @param columnid
	 * @param start
	 * @param len
	 * @return
	 */
	public static ArrayList<String> getAritcleList(String aritcleUrl,
			String columnid, String start, String len,Context mContext) {
		ArrayList<String> list = new ArrayList<String>();

		JSONArray jsonArray = null;
		JSONObject jobj = null;
		JSONObject jobj2 = null;

		String url = aritcleUrl + start + "-" + len + ".html";
		String results = HttpUtils.getResponseFromGetUrl(url, 30000,mContext);
		try {
			if (results != null && !results.equals("")) {
				jobj = new JSONObject(results);
				if(jobj != null) {
					jsonArray = jobj.getJSONArray(columnid);
					for (int j = 0; jsonArray != null && j < jsonArray.length(); j++) {
						jobj2 = jsonArray.getJSONObject(j);	
						list.add(jobj2.getString(GetColumnsInfo.DOCID_TAG));
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
				
		return list;
	}


	/**
	 * 获取最新的投票数据
	 * @param id
	 * @return
	 */
//	@SuppressWarnings("unchecked")
//	public static List<Map<String, Object>> getVoteItemList(String id,Context mContext) {
//		Map<String, Object> m =  getMapItem(id,mContext);
//		List<Map<String, Object>> itemList = (List<Map<String, Object>>) m.get(StringUtil.VOTE_ITEM_LIST);
//		return itemList;
//	}
	
}
